
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Tama_Chan.Utils;


namespace Tama_Chan.ParticleEngine_ns
{

   public class RotatingParticle : IParticle
   {
      public SpriteBatch SpriteBatch { get; set; }//
      public bool IsDead { get; set; }//
      public long CreationTime { get; set; }//
      public long ActivationDelay { get; set; }//
      public bool IsActivated { get; set; }//
      public long ActivationTime { get; set; }
      public long LifeTime { get; set; }
      public Texture2D Texture { get; set; }
      public Vector2 Position { get; set; }
      public Vector2 Velocity { get; set; }
      public Color Color { get; set; }
      public float Rotation { get; set; }
      public float RotationRate { get; set; }
      public float Depth { get; set; }
      public float Scale { get; set; }


      public RotatingParticle(SpriteBatch spriteBatch, 
                              GameTime gameTime, 
                              long activationDelay, 
                              long lifeTime, 
                              Texture2D texture, 
                              Vector2 position, 
                              Vector2 velocity, 
                              Color color, 
                              float rotationRate, 
                              float depth, 
                              float scale)
      {
         SpriteBatch = spriteBatch;
         IsDead = false;
         CreationTime = gameTime.TotalGameTime.Ticks; 
         ActivationDelay = activationDelay;
         if(activationDelay == 0)
         {
            ActivationTime = gameTime.TotalGameTime.Ticks;
            IsActivated = true;
         }
         LifeTime = lifeTime;
         Texture = texture;
         Position = position;
         Velocity = velocity;
         Color = color;
         RotationRate = rotationRate;
         Depth = depth;
         Scale = scale;
      }


      public void Update(GameTime gameTime)
      {
         if(!IsActivated && gameTime.TotalGameTime.Ticks - CreationTime > ActivationDelay)
         {
            ActivationTime = gameTime.TotalGameTime.Ticks;
            IsActivated = true;
         }

         if(!IsActivated || IsDead)
         {
            return;
         }

         if(gameTime.TotalGameTime.Ticks - ActivationTime > LifeTime)
         {
            IsDead = true;
            return;
         }

         float elapsedSeconds = GeneralUtils.GetSecondsFromTicks(gameTime.ElapsedGameTime.Ticks);
         float rotationDelta = RotationRate * elapsedSeconds;
         float updatedRotation = Rotation + rotationDelta;
         Rotation = MathHelper.WrapAngle(updatedRotation);
         Position += Velocity;
      }


      public void Draw()
      {
         SpriteBatch.Draw(Texture,            // Texture
                          Position,           // Position
                          null,               // Source Rectangle
                          Color,              // Color
                          Rotation,           // Rotation
                          new Vector2(Texture.Width / 2f, Texture.Height / 2f), // Origin is center of Texture
                          Scale,              // Current Scale
                          SpriteEffects.None, // Mirroring options
                          Depth);             // z-depth
      }

   }
}
